package com.lagou.mr.wc;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

// 封装任务并提交运行
// 注意：1.要人为手动把hadoop.dll文件需要放到hadoop指定位置去，hadoop.dll文件在正式班入班必备资料里面的‘windows安装hadoop的bin所需文件’文件夹下，不放的话程序运行找不到，
// 会报“org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Ljava/lang/String;I)Z”这个异常
// 2.千万别把类似于Text等类的包倒错了，mr程序里面很多类的导包路径都极其相似，我们需要导入新版的包路径，不然会报类型转换异常
public class WordCountDriver {
    private static final Logger log = LoggerFactory.getLogger(WordCountDriver.class);

    public static void main(String[] args) throws InterruptedException, IOException, ClassNotFoundException {
        // 1.获取配置文件对象，获取Job对象实例
        Configuration conf = new Configuration();
        Job job = null;
        try {
            job = Job.getInstance(conf, "WordCountDriver");
        } catch (IOException e) {
            System.out.println(e.toString());
            e.printStackTrace();
        }
        // 2 指定程序jar的本地路径
        job.setJarByClass(WordCountDriver.class);
        // 3 指定Mapper/Reducer类
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);
        // 4 指定Mapper输出的kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        // 5 指定最终输出的kv数据类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        // 6 指定job处理的原始数据路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        // 7 指定job结果输出路径
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        log.info(WordCountMapper.class.getName()+" Job Status to Running");
        //8 提交作业
        boolean flag = job.waitForCompletion(true);
        // jvm退出：正常退出是0，非0则是错误退出
        System.exit(flag ? 0 : 1);
    }
}
